package com.bort.system.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bort.system.model.Menu;
import com.bort.system.model.vo.MenuVo;
import com.bort.system.service.MenuService;
import com.bort.system.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/menu")
public class AdminMenuController {

    @Resource
    MenuService menuService;

    @GetMapping("/list")
    public Result<Page<Menu>> index(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rowSize", defaultValue = "12") Integer rowSize
    ) {
        Result<Page<Menu>> result = new Result<>();
        Page<Menu> pager = menuService.selectAll(page,rowSize,name);
        result.setData(pager);
        return result;
    }

    @PostMapping("/save")
    public Result save(@RequestBody Menu menu) {
        if (menu.getId() != null && menu.getId() > 0) {
            menuService.update(menu);
        } else {
            menuService.add(menu);
        }
        return new Result();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result delete(@RequestParam Integer id) {
        menuService.delete(id);
        return new Result();
    }

    //
    private List<Menu> getOptions(List<Menu> list) {
        List<Menu> data = new ArrayList<>();
        for (Menu menu : list) {
            if (menu.getLevel() <= 2) {
                data.add(menu);
            }
        }
        return data;
    }

    // 获取下拉框的数据
    private List<MenuVo> getOptions1(String args) {
        List<MenuVo> list = JSONArray.parseArray(args, MenuVo.class);
        List<MenuVo> data = new ArrayList<>();
        for (MenuVo menu : list) {
            if (menu.getParentId() == 0) {
                ArrayList<MenuVo> children = new ArrayList<>();
                for (MenuVo child : list) {
                    if (child.getParentId() == menu.getId()) {
                        children.add(child);
                    }
                    menu.setChildren(children);
                    data.add(menu);
                }
            }
        }
        return data;
    }

    @RequestMapping("/tree")
    public Result menus() {
        Result result = new Result();
        List<MenuVo> menus = menuService.selectAllMenus();
        List<MenuVo> tree = getTree(0, menus);
        result.setData(tree);
        return result;
    }

    /**
     * 获取子节点
     *
     * @param id      父节点id
     * @param allMenu 所有菜单列表
     * @return 每个根节点下，所有子菜单列表
     */
    public List<MenuVo> getTree(Integer id, List<MenuVo> allMenu) {
        //子菜单
        List<MenuVo> children = new ArrayList<MenuVo>();
        for (MenuVo nav : allMenu) {
            // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
            //相等说明：为该根节点的子节点。
            if (nav.getParentId().equals(id)) {
                children.add(nav);
            }
        }
        //递归
        for (MenuVo nav : children) {
            nav.setChildren(getTree(nav.getId(), allMenu));
        }
//        Collections.sort(children,order());//排序
        //如果节点下没有子节点，返回一个空List（递归退出）
        if (children.size() == 0) {
            return new ArrayList<MenuVo>();
        }
        return children;
    }
}
